#pragma once 
#include <string>
#include <sys/types.h>
#include <sys/socket.h>


// \r\n  \n  \r
class Utility{
    public:
    static int ReadLine(int sock,std::string& line){
        char ch = 0;
        while(ch != '\n'){
            ssize_t s = recv(sock,&ch,1,0);
            if(s> 0){
                if(ch == '\r'){
                    recv(sock,&ch,1,MSG_PEEK);
                    if(ch == '\n'){
                        recv(sock,&ch,1,0);
                    }
                    else{
                        ch = '\n';
                    }
                }
                line.push_back(ch);
            } 
            else if(s == 0){
                return 0;
            }
            else{
                return -1;
            }
        }
        return line.size();
    }
    static bool CutString(std::string& target,std::string& key,std::string& out,const std::string& sep){
        size_t pos = target.find(sep);
        if(pos != std::string::npos){
            key = target.substr(0,pos);
            out = target.substr(pos+sep.size());
           return true;
        }
        return false;
    }
};
